package com.atguigu.gulimall.order.listener;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author: xuesm
 * @date: 2025/3/5 17:27
 */
@Service
@RabbitListener(queues = "order.release.order.queue")
public class OrderClosedListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void listenOrderClose(OrderEntity order, Message message, Channel channel) throws IOException {
        System.out.println("收到过期订单的信息， 准备关单，OrderSn：" + order.getOrderSn());
        try {
            orderService.closeOrder(order);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }

    }
}
